package com.example.day03_mvp;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager;

import com.example.day03_mvp.fragment.HomeFragment;
import com.example.day03_mvp.fragment.WebFragment;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;

public class MainActivity extends AppCompatActivity {

    private FrameLayout mMainFrame;
    private TabLayout mMainTab;
    private TextView mMianText;
    private Toolbar mMainBar;
    private LinearLayout mMianLin;
    private NavigationView mMainBavig;
    private DrawerLayout mMainDrawer;
    private HomeFragment homeFragment;
    private WebFragment webFragment;
    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initbar();
    }

    private void initbar() {
        mMainBar.setTitle("");
        setSupportActionBar(mMainBar);
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, mMainDrawer, mMainBar, R.string.app_name, R.string.app_name);
        actionBarDrawerToggle.syncState();
        mMainDrawer.addDrawerListener(actionBarDrawerToggle);
        mMainDrawer.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                mMianLin.setX(mMainBavig.getWidth()*slideOffset);
            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
    }

    private void initViews() {
        mMainFrame = findViewById(R.id.main_frame);
        mMainTab = findViewById(R.id.main_tab);
        mMianText = findViewById(R.id.mian_text);
        mMainBar = findViewById(R.id.main_bar);
        mMianLin = findViewById(R.id.mian_lin);
        mMainBavig = findViewById(R.id.main_bavig);
        mMainDrawer = findViewById(R.id.main_drawer);
        homeFragment = new HomeFragment();
        webFragment = new WebFragment();
        manager = getSupportFragmentManager();
        manager.beginTransaction().add(R.id.main_frame,homeFragment).show(homeFragment).add(R.id.main_frame,webFragment).hide(webFragment).commit();
        mMainTab.addTab(mMainTab.newTab().setText("首页"));
        mMainTab.addTab(mMainTab.newTab().setText("web"));
        mMianText.setText("首页");
        mMainTab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()){
                    case 0:
                        manager.beginTransaction().show(homeFragment).hide(webFragment).commit();
                        mMianText.setText("首页");
                        break;
                    case 1:
                        manager.beginTransaction().show(webFragment).hide(homeFragment).commit();
                        mMianText.setText("web");
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
